<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * Football Referees extension for Joomleague
 * @version $Id:playersranking.php 79 2008-02-12 15:58:00Z julien.vonthron $
 * @package JlgFootballReferees
 * @copyright Copyright (C) 2007 Julien Vonthron. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * JlgFootballReferees is free software and parts of it may contain or be derived from the
 * GNU General Public License or other free or open source software licenses.
 * 
 * contact: julien.vonthron[at]gmail.com 
 *   
 * Joomleague is a League Management and Prediction Game for Joomla!
 * Homepage: http://www.joomleague.de 
 *  
 */
?>

<?php

class ModelProjectReferee {

  var $_ref = null;
  var $_project;
  var $_db;
  var $_ref_id;
	
  var $_games = null;
  
  function ModelProjectReferee( &$database, &$project, $referee_id )
  {
  	if (!$referee_id) {
  		echo "Error: referee id is 0 !";
  		return null;
  	}
  	$this->_db = $database;
    $this->_project = $project;
    $this->_ref_id = $referee_id;
  }
  
  function getReferee()
  {    
  	if ( !$this->_ref ) {
    	// get referee of the project
	    $query = ' SELECT r.* FROM #__joomleague_referees as r'
	           . ' WHERE r.id = ' .$this->_ref_id;
	          
	    $this->_db->setQuery($query);
	    $this->_ref = null;
      $this->_db->loadObject($this->_ref);      
    }
    return $this->_ref;
  }
  
  function getGames()
  {
  	if ( !$this->_games ) {      
	    $query = ' SELECT m.*, r.matchcode, p.name as project_name '
	           . ' FROM #__joomleague_matches AS m '
	           . ' INNER JOIN #__joomleague_rounds AS r ON r.id = m.round_id '
             . ' INNER JOIN #__joomleague AS p ON p.id = m.project_id '
	           . ' WHERE m.referee_id='.$this->_ref_id
	           . ' AND m.published = 1 ';
	    $query .= ' AND m.project_id='.$this->_project->id;

	    $query .= ' ORDER BY m.match_date ASC';
	    $this->_db->setQuery($query);
	    $this->_games = $this->_db->loadObjectList();
  	}
    return $this->_games;
  }
  
  /**
   * return array of events (properties, stats, sum)
   *
   * @param int array $event_ids
   * @return array of object events (properties, stats, sum)
   */
  function getEvents($event_ids)
  {  
    $events = array();
    if (count($event_ids))
    {
      foreach($event_ids AS $ev)
      {
        $event = new stdclass;
        $event->eventtype = REHelper::getEventType($ev);
        $event->stats = $this->getEventGamesCount($ev);
        $sum = 0;
        foreach ( $event->stats AS $s ) {
          $sum += $s->sum;
        }
        $event->sum = $sum;
        $events[$ev] = $event;
      }
    }
    return $events;
  }
  
  function getEventGamesCount($event_id)
  {
    if ( !$this->_games ) return null;
    $ids = array();
    foreach($this->_games AS $g) {
      $ids[]=$g->match_id;
    }
    
    $query = ' SELECT SUM(event_sum) as `sum`, m.match_id '
             . ' FROM #__joomleague_matches as m '
             . ' INNER JOIN #__joomleague_match_events AS me ON me.match_id = m.match_id '
             . ' WHERE m.match_id IN ('.implode(",", $ids).') '
             . ' AND me.event_type_id = ' . $event_id
             . ' GROUP BY m.match_id ';
    $this->_db->setQuery($query);
    return $this->_db->loadObjectList('match_id');
  }
}
?>
